package com.mcxx.util;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Base64ImageUtil {

    public static void main(String[] args) throws Exception {
        String base64 = "/9j/4AAQSkZJRgABAQEAYABgAAD" +
				"/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL" +
				"/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAB" +
				"+AGYDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL" +
				"/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigAoprusaM7sFVRkk9BXnXij4vaTpCvBpS/2hcjjcDiNfx7/hQB6JJNFCu6WRI19WYAViX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5hGOPavPNd1O71PxHbW9xKxjYMxHbjH+NbpOxUUAYA4HpXJOs3dI15EtxLvLwuvYivJrBJp/HAtXZhHaSEqv0xXq8su0Y715d4ruD4f8XW+o24y06FpE6A84NY09ZM02R6xCAcCpHgy3TisLw74ig1u086OGSNlwGBx1/OuiWTJCnOTVJG1yLyABUTQgmrjMFXJzUQYFyBnjrQ4oLldo9qkVy3iK3E09uByVkVvyNdNf3qWkMs7qzLGDkCuI0/XTr99PMsflwxsAit1IPrUSjZCbOtgYLFgVtaLrT6dJskJa3Y8j+77iufjlRUxg8elRy3yx8gMamEmndGLV3Y9at7mG6jEkEiup7g0V5Lb6xPES8MksJb/nm5XI/A0V1rEaaoPq77n/2Q==";
        String url = "d://";
        GenerateImage(base64, url);


    }


    public static String GetImageStr(byte[] data) {

        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }


    public static String GetImageStr(String imgFile) {
        InputStream in = null;
        byte[] data = null;

        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }


    public static boolean GenerateImage(String imgStr, String imgFilePath) throws Exception {
        if (imgStr == null)
        {
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();


        byte[] b = decoder.decodeBuffer(imgStr);
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }

        imgFilePath = "d://temp_kjbl_001_ab_010.jpg";
        OutputStream out = new FileOutputStream(imgFilePath);
        out.write(b);
        out.flush();
        out.close();
        return true;
    }


    public static boolean GenerateImage2(String imgStr, String path) throws Exception {
        if (imgStr == null) {
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();


        byte[] b = decoder.decodeBuffer(imgStr);
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }


        OutputStream out = new FileOutputStream(new File(path));
        out.write(b);
        out.flush();
        out.close();
        return true;
    }


    public static String GetImageStr2(MultipartFile imgFile) {
        InputStream in = null;
        byte[] data = null;

        try {
            in = imgFile.getInputStream();
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }


    public static String NetImageToBase64(String netImagePath) {
        String strNetImageToBase64 = null;
        final ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {

            URL url = new URL(netImagePath);
            final byte[] by = new byte[1024];

            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);


            InputStream is = conn.getInputStream();

            int len = -1;
            while ((len = is.read(by)) != -1) {
                data.write(by, 0, len);
            }

            BASE64Encoder encoder = new BASE64Encoder();
            strNetImageToBase64 = encoder.encode(data.toByteArray());
            System.out.println("网络图片转换Base64:" + strNetImageToBase64);

            is.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

        return strNetImageToBase64;
    }
}

